package com.fw.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.fw.dao.RoleDao;
import com.fw.model.Permission;
import com.fw.model.Role;
import com.fw.util.RolePermissonsZtree;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Record;

public class RoleDaoImpl implements RoleDao {

	@Override
	public boolean add_roleDao(Role role) {
		return role.save();
	}

	@Override
	public Role query_roleByRoleNameDao(Role role) {
		String s = "select * from role r where r.r_name = ?";
		List<Role> rs = Role.roleDao.find(s, role.getStr("r_name"));
		if(rs != null && rs.size()>0) return rs.get(0);
		return null;
	}

	@Override
	public Integer query_totalRoleDao(String sql) {
		String s = "select count(*) from role r where 1 = 1 "+ sql;
		long count  = Db.queryLong(s);
		return (int) count;
	}

	@Override
	public List<Role> query_allRoleByPageDao(String sql, int firstPage,int maxPage) {
		String s = "select * from role r where 1 = 1 limit ?,?";
		List<Role> rs = Role.roleDao.find(s,firstPage,maxPage);
		if(rs != null && rs.size() > 0) return rs;
		return null;
	}

	@Override
	public boolean delete_roleByIdDao(Role role) {
		boolean flag = Db.deleteById("role", "r_id",role.getInt("r_id"));
		return flag;
	}

	@Override
	public Role query_roleByIdDao(Role role) {
		String sql = "select * from role r where r.r_id = ?";
		List<Role> rs = Role.roleDao.find(sql, role.getInt("r_id"));
		if(rs != null && rs.size()>0) return rs.get(0);
		return null;
	}

	@Override
	public boolean update_roleByIdDao(Role role) {
		Record r = Db.findById("role", "r_id",role.getInt("r_id"))
				.set("r_name", role.getStr("r_name"))
				.set("r_status", role.getInt("r_status"));
		return Db.update("role","r_id", r);
	}
	
	@Override
	public List<Role> query_allRoleDao() {
		String s = "select * from role";
		List<Role> rs = Role.roleDao.find(s);
		if(rs != null && rs.size() > 0) return rs;
		return null;
	}

	@Override
	public List<RolePermissonsZtree> query_rolePermissonsZtreeDataDao(Role role) {
		String sql = "select p.p_id,p.p_name,p.parent_id,rp.rp_id from permission p left join role_permission rp on rp.p_id = p.p_id and rp.r_id = ?";
		List<Record> os = Db.find(sql, role.getInt("r_id"));
		List<RolePermissonsZtree> rpzs = null;
		if(os.size()>0){
			rpzs = new ArrayList<RolePermissonsZtree>();
			for(int i = 0;i<os.size();i++){
				RolePermissonsZtree rpz = new RolePermissonsZtree();
				Permission permission = new Permission();
				permission.put("p_id",os.get(i).getInt("p_id"));
				permission.put("p_name",os.get(i).getStr("p_name"));
				permission.put("parent_id", os.get(i).getInt("parent_id"));
				rpz.setPermission(permission);
				if(os.get(i).getInt("rp_id") != null){
					rpz.setChecked(true);
				}else{
					rpz.setChecked(false);
				}
				rpzs.add(rpz);
			}
		}
		return rpzs;
	}

	@Override
	public boolean delete_rolePermissionDao(Role role) {
		String sql = "delete role_permission rp where rp.r_id = ?";
		int i = Db.update(sql, role.getInt("r_id"));
		if(i>0) return true;
		return false;
	}

	@Override
	public boolean update_rolePermissionDao(Role role,List<Permission> permissions) {
		boolean flag = false;
		final Role r = role;
		final List<Permission> ps = permissions;
		flag = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				List<Permission> ps2 = r.getPermissionsByRole();
				if(ps2.size() > 0){
					int dele = Db.update("delete from role_permission where r_id = ?", r.getInt("r_id"));
					if(dele <=0) return false;
				}
				System.out.println(ps);
				for(int i = 0;i<ps.size();i++){
					final Record rp = new Record().set("r_id",r.getInt("r_id"))
							.set("p_id",Integer.valueOf(ps.get(i).getStr("p_id")));
					boolean s = Db.save("role_permission", rp);
					if(!s) return false;
				}
				return true;
			}
		});
		return flag;
	}

}
